<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ArrayList 扩容机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            bottom: -20px;
            width: 2px;
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f3e7ff 0%, #e9d5ff 100%);
            border-left: 4px solid #764ba2;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .image-container {
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 20px 0;
            transition: transform 0.3s ease;
        }
        
        .image-container:hover {
            transform: scale(1.02);
        }
        
        .image-container img {
            width: 100%;
            height: auto;
            display: block;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    ArrayList 扩容机制
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析与源码剖析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索 Java 集合框架中最常用数据结构的动态扩容策略
                </p>
                <div class="mt-10 flex justify-center space-x-8">
                    <div class="text-center">
                        <div class="text-4xl font-bold">1.5x</div>
                        <div class="text-sm opacity-75">扩容倍数</div>
                    </div>
                    <div class="text-center">
                        <div class="text-4xl font-bold">O(n)</div>
                        <div class="text-sm opacity-75">时间复杂度</div>
                    </div>
                    <div class="text-center">
                        <div class="text-4xl font-bold">10</div>
                        <div class="text-sm opacity-75">默认容量</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- 核心概念 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                <span class="drop-cap">A</span>rrayList 的扩容机制是通过将容量扩大为原容量的 1.5 倍来减少扩容频率，扩容时创建一个新的更大的数组，并将现有元素复制到新数组中。虽然扩容操作的性能开销是 O(n)，但其策略旨在平衡内存使用和性能。扩容的设计使得 ArrayList 能够动态调整容量以适应不同的存储需求。
            </p>
        </div>

        <!-- 扩容机制可视化 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">扩容机制可视化</h2>
            <div class="mermaid">
                graph LR
                    A[初始数组<br/>容量: 10] -->|添加第11个元素| B[触发扩容]
                    B --> C[计算新容量<br/>10 + 10>>1 = 15]
                    C --> D[创建新数组<br/>容量: 15]
                    D --> E[复制元素]
                    E --> F[更新引用]
                    F --> G[完成扩容<br/>新容量: 15]
                    
                    style A fill:#f3e7ff,stroke:#764ba2,stroke-width:2px
                    style B fill:#ffeaa7,stroke:#fdcb6e,stroke-width:2px
                    style C fill:#dfe6e9,stroke:#74b9ff,stroke-width:2px
                    style D fill:#dfe6e9,stroke:#74b9ff,stroke-width:2px
                    style E fill:#dfe6e9,stroke:#74b9ff,stroke-width:2px
                    style F fill:#dfe6e9,stroke:#74b9ff,stroke-width:2px
                    style G fill:#d1f2eb,stroke:#00b894,stroke-width:2px
            </div>
        </div>

        <!-- 扩容触发条件 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-rocket mr-3 text-purple-600"></i>扩容触发条件
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-plus-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">元素添加</h3>
                    <p class="text-gray-600">当 ArrayList 的元素数量达到当前数组的容量时，尝试添加新元素将触发扩容。</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-expand-arrows-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">容量不足</h3>
                    <p class="text-gray-600">如果在添加元素时发现当前容量不足，则需要扩容以容纳新元素。</p>
                </div>
            </div>
        </div>

        <!-- 扩容过程详解 -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>扩容过程详解
            </h2>
            
            <div class="space-y-6">
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">1. 确定新容量</h3>
                    <p class="text-gray-600 mb-4">ArrayList 扩容时，通常会将容量扩大为原容量的 1.5 倍（即 50% 增长）。这种增长策略有助于减少频繁的扩容操作，平衡了内存使用和性能开销。</p>
                    
                    <div class="code-block p-6">
                        <pre><code>private void ensureCapacity(int minCapacity) {
    // 获取当前数组的容量
    int oldCapacity = elementData.length;
    // 如果当前容量小于最小容量，则需要扩容
    if (minCapacity > oldCapacity) {
        // 新容量通常是当前容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        // 确保新容量至少满足最小容量要求
        if (newCapacity < minCapacity) {
            newCapacity = minCapacity;
        }
        // 扩容
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
}</code></pre>
                    </div>
                    
                    <div class="highlight-box mt-4">
                        <p class="text-gray-700"><i class="fas fa-lightbulb text-yellow-500 mr-2"></i><strong>技术细节：</strong>1.5 倍是通过位运算的右移操作计算的，即 <code class="bg-gray-200 px-2 py-1 rounded">oldCapacity >> 1</code> 是 <code class="bg-gray-200 px-2 py-1 rounded">oldCapacity / 2</code>，将其加到 oldCapacity 上形成新容量。</p>
                    </div>
                </div>

                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">2. 创建新数组</h3>
                    <p class="text-gray-600">在扩容时，ArrayList 会创建一个新的、更大的数组，大小为原数组容量的 1.5 倍或满足最小容量的要求。</p>
                </div>

                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">3. 复制元素</h3>
                    <p class="text-gray-600">ArrayList 会将原数组中的所有元素复制到新的数组中。这个过程是通过 <code class="bg-gray-200 px-2 py-1 rounded">Arrays.copyOf</code> 方法完成的，时间复杂度为 O(n)，其中 n 是当前元素的数量。</p>
                </div>

                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">4. 更新引用</h3>
                    <p class="text-gray-600">将原数组的引用更新为新数组的引用。这样，ArrayList 就会使用新数组来存储后续的元素